package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.param.AppInfoVO;
import com.yugao.fintech.draper.admin.param.AppInfoQuery;
import com.yugao.fintech.draper.admin.service.AppInfoService;
import com.yugao.fintech.draper.core.constant.ValidationGroup;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.core.util.ValidationUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app")
@RequiredArgsConstructor
@Tag(name = "第三方应用管理")
public class AppInfoController {
    private final AppInfoService appInfoService;

    @Operation(summary = "查询应用列表分页")
    @GetMapping(value = "/page")
    public ResultVO<PageResult<AppInfoVO>> pageQuery(@RequestBody AppInfoQuery appInfoQuery) {
        ValidationUtil.validateParam(appInfoQuery, ValidationGroup.query.class);
        return ResultVO.success(PageResult.success(appInfoService.pageQuery(appInfoQuery)));
    }

    @Operation(summary = "获取应用详细信息")
    @GetMapping(value = "/findById")
    public ResultVO<AppInfoVO> findById(Long id) {
        return ResultVO.success(appInfoService.findById(id));
    }

    @Operation(summary = "获取全部应用")
    @GetMapping(value = "/findAll")
    public ResultVO<List<AppInfoVO>> findAll() {
        return ResultVO.success(appInfoService.findAll());
    }

    @Operation(summary = "添加应用")
    @PostMapping(value = "/add")
    public ResultVO<Boolean> add(@RequestBody AppInfoVO appInfoVO) {
        ValidationUtil.validateParam(appInfoVO, ValidationGroup.add.class);
        return ResultVO.success(appInfoService.addApp(appInfoVO));
    }

    @Operation(summary = "修改应用")
    @PostMapping(value = "/update")
    public ResultVO<Boolean> update(@RequestBody AppInfoVO appInfoVO) {
        ValidationUtil.validateParam(appInfoVO, ValidationGroup.update.class);
        return ResultVO.success(appInfoService.updateApp(appInfoVO));
    }

    @Operation(summary = "删除应用")
    @DeleteMapping(value = "/delete")
    public ResultVO<Boolean> delete(Long id) {
        return ResultVO.success(appInfoService.deleteByAppId(id));
    }
}